#include "audiosettingsdialog.h"
#include "ui_audiosettingsdialog.h"

#include "mainwindow.h"

AudioSettingsDialog::AudioSettingsDialog(void* mainWindow,QWidget* parent):QDialog(parent),ui(new Ui::AudioSettingsDialog){
  ui->setupUi(this);

  this->mainWindow=mainWindow;
  IsInit=false;

  ui->comboBox_SampleRate->addItem("44100");
  ui->comboBox_SampleRate->addItem("48000");
  ui->comboBox_SampleRate->addItem("88200");
  ui->comboBox_SampleRate->addItem("96000");
  ui->comboBox_SampleRate->addItem("176400");
  ui->comboBox_SampleRate->addItem("192000");
  ui->comboBox_SampleRate->addItem("352800");
  ui->comboBox_SampleRate->addItem("384000");

  ui->comboBox_SampleRate->setCurrentIndex(FindSampleRateIndex((int)((MainWindow*)mainWindow)->GetSampleRate()));


  ui->comboBox_NumOfSampleBit->addItem("16");
  ui->comboBox_NumOfSampleBit->addItem("32");

  ui->comboBox_NumOfSampleBit->setCurrentIndex(FindNumOfSampleBitIndex(((MainWindow*)mainWindow)->GetNumOfSampleBit()));

  ui->horizontalSlider_PulseDiff->setValue(((MainWindow*)mainWindow)->GetPulseLR_Diff());
  ui->label_PulseDiff->setText(QString::number(((MainWindow*)mainWindow)->GetPulseLR_Diff()));


  IsInit=true;
}

AudioSettingsDialog::~AudioSettingsDialog(){
  delete ui;
}

int AudioSettingsDialog::FindSampleRateIndex(int SampleRate){
  
  switch(SampleRate){
    case 44100:
      return 0;
    case 48000:
      return 1;
    case 88200:
      return 2;
    case 96000:
      return 3;
    case 176400:
      return 4;
    case 192000:
      return 5;
    case 352800:
      return 6;
    case 384000:
    default:
      return 7;
  }

  return 7;
}

H_NES_CPP::SampleRate AudioSettingsDialog::GetSampleRateFromIndex(int Index){
  switch(Index){
    case 0:
      return H_NES_CPP::SampleRate::SampleRate_44100;
    case 1:
      return H_NES_CPP::SampleRate::SampleRate_48000;
    case 2:
      return H_NES_CPP::SampleRate::SampleRate_88200;
    case 3:
      return H_NES_CPP::SampleRate::SampleRate_96000;
    case 4:
      return H_NES_CPP::SampleRate::SampleRate_176400;
    case 5:
      return H_NES_CPP::SampleRate::SampleRate_192000;
    case 6:
      return H_NES_CPP::SampleRate::SampleRate_352800;
    case 7:
    default:
      return H_NES_CPP::SampleRate::SampleRate_384000;
  }

  return H_NES_CPP::SampleRate::SampleRate_384000;
}

int AudioSettingsDialog::FindNumOfSampleBitIndex(int NumOfSampleBit){

  switch(NumOfSampleBit){
    case 16:
      return 0;
    case 32:
    default:
      return 1;
  }

  return 1;
}

int AudioSettingsDialog::GetNumOfSampleBitFromIndex(int Index){
  switch(Index){
    case 0:
      return 16;
    case 1:
    default:
      return 32;
  }

  return 1;
}




void AudioSettingsDialog::closeEvent(QCloseEvent* event){

  Q_UNUSED(event);

  ((MainWindow*)mainWindow)->AudioSettingsDialogCloseEvent();
}




void AudioSettingsDialog::on_comboBox_SampleRate_currentIndexChanged(int index){
  
  if(!IsInit){
    return;
  }

  ((MainWindow*)mainWindow)->SetSampleRate(GetSampleRateFromIndex(index));

}

void AudioSettingsDialog::on_comboBox_NumOfSampleBit_currentIndexChanged(int index){
  
  if(!IsInit){
    return;
  }

  ((MainWindow*)mainWindow)->SetNumOfSampleBit(GetNumOfSampleBitFromIndex(index));

}

void AudioSettingsDialog::on_horizontalSlider_PulseDiff_valueChanged(int value){
  
  if(!IsInit){
    return;
  }

  ui->label_PulseDiff->setText(QString::number(value));

  ((MainWindow*)mainWindow)->SetPulseLR_Diff(value);

}



